import App from './App'
import call from '@/static/js/call.js';
import encryTool from '@/static/js/encryTool.js' 
import {deepClone} from '@/static/js/deepClone.js'
import mapUtil from '@/static/js/map-util.js'
import {BASE_URL,gaode,navigatorHeight,cityCode,distance,notificationText} from '@/config.js'
import * as api from '@/api/api.js'; 
import { 	pathToBase64, base64ToPath 	} from 'image-tools'

uni.$Call = call
uni.$EncryTool = encryTool
uni.$api = api
uni.$baseUrl = BASE_URL
uni.$gaode = gaode
uni.$navigatorHeight = navigatorHeight
uni.$cityCode = cityCode
uni.$mapUtil = mapUtil
uni.$distance = distance
uni.$notificationText = notificationText

Vue.prototype.$Call = call;
Vue.prototype.$EncryTool = encryTool;
Vue.prototype.$api = api;
Vue.prototype.$baseUrl = BASE_URL;
Vue.prototype.$gaode = gaode;
Vue.prototype.$navigatorHeight = navigatorHeight;
Vue.prototype.$cityCode = cityCode;
Vue.prototype.$mapUtil = mapUtil;
Vue.prototype.$deepClone = deepClone;
Vue.prototype.$distance = distance;
Vue.prototype.$notificationText = notificationText;

Vue.prototype.$init = async function() {
	// 加密
	// this.currentTime = new Date().getTime()
	// this.encrytoolTime = uni.$EncryTool.encodeBase64(this.currentTime + "");
	// let encrytoolUsername = uni.$EncryTool.encodeAES(this.username, uni.$Call.aesKey + this.currentTime)
	// let encrytoolPassword = uni.$EncryTool.encodeAES(this.password, uni.$Call.aesKey + this.currentTime)
	
	// 解密
	// const encrytoolTime = uni.$EncryTool.decryptBase64(res.data.timestamp + "");
	// const newData = uni.$EncryTool.decryptAES(res.data.data, uni.$Call.aesKey + encrytoolTime)
	// const jsonData = JSON.parse(newData);
	
	// 本地存储
	// uni.setStorageSync(uni.$Call.token, jsonData.token)
	// uni.setStorageSync(uni.$Call.loginData, jsonData)
	
	// 保存登录状态到本地
	// uni.setStorageSync('isLoggedIn', true);
}

Vue.prototype.$previewImage =  function( url) {
	 if(url == undefined || url == 'undefined'){
		uni.showToast({
			icon:'error',
			title:'图片地址为空'
		});
		return;
	 }
	
	 if(url.indexOf("base64") < 0 && url.indexOf("file://") != 0 && url.indexOf("http") != 0 ){
		 //不是base64 不是file://开头的,不是http和https开头的
		 //是回显的图片地址，未拼接项目域名的
		url = BASE_URL + url;  
	 }  
	if(url.indexOf("http") == 0){
		//是http开头的下载完成之后显示文件，因为项目域名是https的没有证书，直接显示无法显示
		uni.showLoading({
			title:'加载图片',
			mask: true
		})
		uni.downloadFile({
			url: url,
			success: (res)=> {
				if (res.statusCode === 200) {
					uni.hideLoading(); 
					uni.previewImage({
						urls:[res.tempFilePath],
						success() {
							return;
						}
					})
				} else{
					uni.showToast({
						title:'获取图片失败!',
						icon:'error'
					})
				}
			},
			fail(){
				uni.showToast({
					title:'获取图片失败!',
					icon:'error'
				})
			}
		});
	} else if(url.indexOf(";base64") >= 0 ){ 
		uni.showLoading({
			title:'加载图片',
			mask: true
		})
		base64ToPath(url, '.png').then((resInfo)=>{
			uni.hideLoading(); 
			uni.getImageInfo({
				  src: resInfo,
				  success: function(res){
					uni.previewImage({
						urls:[res.path]
					});
				  },
			})
		})
	} else{
		//其他的地址的直接显示即可
		uni.previewImage({
			urls:[url],
			success() {
				return;
			}
		})
	}
	 
}


// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
Vue.config.silent = false
App.mpType = 'app'
const app = new Vue({
	...App
})
app.$mount()
// #endif

// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif